﻿using System;
using System.Collections.Generic;
using System.Data;
using BusinessNews;
using DataNews.StandartInterfaces;
using DataObjects.ADO;

namespace DataNews.ADO.SqlServer.Dictionary
{
    class SqlServerNewsLogDao: INewsLog
    {
        private object[] Take(NewsLog info)
        {
            return new object[]
             {
                 "@Id",info.Id,"@Title",info.Title,"@Content",info.Content,"@IP",info.Ip,"@Location",info.Location,"@Device",info.Device,"@CreatedBy",info.CreatedBy,"@CreatedDate",info.CreatedDate,"@Type",info.Type
			
             };
        }
        /*
        private object[] Take(long id, string Title, string content, string iP, string location, string device, string createdBy, DateTime createdDate, string type)
        {
            return new object[]
             {
                 "@Id",id,"@Title",Title,"@Content",content,"@IP",iP,"@Location",location,"@Device",device,"@CreatedBy",createdBy,"@CreatedDate",createdDate,"@Type",type
			
             };
        }
        private object[] Take( string Title, string content, string iP, string location, string device, string createdBy, DateTime createdDate, string type)
        {
            return new object[]
             {
                "@Title",Title,"@Content",content,"@IP",iP,"@Location",location,"@Device",device,"@CreatedBy",createdBy,"@CreatedDate",createdDate,"@Type",type
			
             };
        }
        */
        private object[] Take(long id)
        {
            return new object[]
             {
                 "@Id",id
			
             };
        }

        private static Func<IDataReader, NewsLog> Make = reader =>
                                                             {
                                                                 var obj = new NewsLog();
                                                                 if (!reader.IsDBNull(0))
                                                                 {
                                                                     obj.Id = (long) reader["Id"];
                                                                 }
                                                                 if (!reader.IsDBNull(1))
                                                                 {
                                                                     obj.Title = (string) reader["Title"];
                                                                 }
                                                                 if (!reader.IsDBNull(2))
                                                                 {
                                                                     obj.Content = (string) reader["Content"];
                                                                 }
                                                                 if (!reader.IsDBNull(3))
                                                                 {
                                                                     obj.Ip = (string) reader["IP"];
                                                                 }
                                                                 if (!reader.IsDBNull(4))
                                                                 {
                                                                     obj.Location = (string) reader["Location"];
                                                                 }
                                                                 if (!reader.IsDBNull(5))
                                                                 {
                                                                     obj.CreatedBy = (string) reader["CreatedBy"];
                                                                 }
                                                                 if (!reader.IsDBNull(7))
                                                                 {
                                                                     obj.CreatedDate = (DateTime) reader["CreatedDate"];
                                                                 }
                                                                 if (!reader.IsDBNull(8))
                                                                 {
                                                                     obj.Type = (string) reader["Type"];
                                                                 }
                                                                 return obj;
                                                             };
        public object CreateNewsLog(NewsLog info)
        {
            return Db.Insert("usp_CreateNewsLog", CommandType.StoredProcedure, Take(info));
        }
        /*
        public object CreateNewsLog(string Title, string content, string iP, string location, string device, string createdBy, DateTime createdDate, string type)
        {
            return Db.Insert("usp_CreateNewsLog", CommandType.StoredProcedure, Take(Title, content, iP, location, device, createdBy, createdDate, type));
        }
        */
        public object UpdateNewsLog(NewsLog info)
        {
            return Db.Update("usp_UpdateNewsLog", CommandType.StoredProcedure, Take(info));
        }
        /*
        public object UpdateNewsLog(int id,string Title, string content, string iP, string location, string device, string createdBy, DateTime createdDate, string type)
        {
            return Db.Update("usp_UpdateNewsLog", CommandType.StoredProcedure, Take(id, Title, content, iP, location, device, createdBy, createdDate, type));
        }
        */
        public object DeleteNewsLog(long id)
        {
            return Db.Update("uspNewsLog_Delete", CommandType.StoredProcedure, Take(id));
        }

        public object DeleteNewsLog(string id)
        {
            object[] idList = {"@IdList", id};
            return Db.Update("uspNewsLog_DeleteInId", CommandType.StoredProcedure, idList);
        }

        public NewsLog GetNewsLog(long id)
        {
            return Db.Read("uspNewsLog_Get", CommandType.StoredProcedure, Make, Take(id));
        }
        public List<NewsLog> GetAllNewsLog()
        {
            return Db.ReadList("uspNewsLog_GetList", CommandType.StoredProcedure, Make);
        }

        public List<NewsLog> GetPagingNewsLog(int currentPage, ref int totalRows)
        {
            object[] parms = { "@currentPage", currentPage, "totalRows", totalRows };
            return Db.ReadList("uspNewsLog_GetPaging", CommandType.StoredProcedure, Make, out totalRows, parms);
        }
    }
}
